﻿Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Public Delegate Sub TeamRoutedEventHandler(ByVal sender As Object, ByVal e As TeamRoutedEventArgs)


Partial Public Class SelectionPanel

    Public Event TeamSelectionChanged As TeamRoutedEventHandler

#Region " Routed Events "

    Public Shared ReadOnly TeamSelectionEvent As RoutedEvent = EventManager.RegisterRoutedEvent("TeamSelection", RoutingStrategy.Tunnel, GetType(TeamRoutedEventHandler), GetType(SelectionPanel))

    ' Provide CLR accessors for the event
    Public Custom Event TeamSelection As TeamRoutedEventHandler
        AddHandler(ByVal value As TeamRoutedEventHandler)
            Me.AddHandler(TeamSelectionEvent, value)
        End AddHandler

        RemoveHandler(ByVal value As TeamRoutedEventHandler)
            Me.RemoveHandler(TeamSelectionEvent, value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As TeamRoutedEventArgs)
            Me.RaiseEvent(e)
        End RaiseEvent

    End Event



#End Region

#Region " TFS "

#Region " Team Server "

    Private Property TeamServer() As String
        Get
            Return My.Settings.TeamServer
        End Get
        Set(ByVal value As String)
            My.Settings.TeamServer = value
            My.Settings.Save()
        End Set
    End Property

    Public ReadOnly Property Server() As TeamFoundationServer
        Get
            If String.IsNullOrEmpty(TeamServer) Then
                Return Nothing
            End If
            Return TeamFoundationServerFactory.GetServer(TeamServer)
        End Get
    End Property

    Private Sub ux_TeamServerDDL_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ux_TeamServerDDL.SelectionChanged
        TeamServer = CType(e.AddedItems(0), TeamFoundationServer).Name
        RefreshTeamProjects()
    End Sub

    Private Sub RefreshTeamServers()
        '-------------------------
        ux_TeamServerDDL.DataContext = Microsoft.TeamFoundation.Client.RegisteredServers.GetServers
        Me.ux_TeamServerDDL.SelectedValue = Server
        TeamProject = String.Empty
        '-------------------------
    End Sub

#End Region

#Region " Team Project "

    Public ReadOnly Property WorkItemStore() As WorkItemStore
        Get
            If Not Server Is Nothing Then
                Try
                    Return DirectCast(Server.GetService(GetType(WorkItemStore)), WorkItemStore)
                Catch ex As Exception
                    'TODO: Handle Errors
                End Try
            End If
            Return Nothing
        End Get
    End Property

    Private Property TeamProject() As String
        Get
            Return My.Settings.TeamProject
        End Get
        Set(ByVal value As String)
            My.Settings.TeamProject = value
            My.Settings.Save()
        End Set
    End Property

    Private ReadOnly Property Project() As Project
        Get
            If Not WorkItemStore Is Nothing And Not String.IsNullOrEmpty(TeamProject) Then
                Return WorkItemStore.Projects(TeamProject)
            End If
            Return Nothing
        End Get
    End Property

    Private Sub RefreshTeamProjects()
        Me.ux_TeamProjectDDL.IsEnabled = False
        '-------------------------
        If Not WorkItemStore Is Nothing Then
            ux_TeamProjectDDL.DataContext = WorkItemStore.Projects
            Me.ux_TeamProjectDDL.SelectedValue = Project
            ux_TeamProjectDDL.IsEnabled = True
        End If
        '-------------------------
    End Sub


    Private Sub ux_TeamProjectDDL_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ux_TeamProjectDDL.SelectionChanged
        If e.AddedItems.Count > 0 Then
            TeamProject = CType(e.AddedItems(0), Project).Name
            Refresh()
        End If
    End Sub

#End Region

#Region " TeamView "

    Private m_TeamViewInital As Boolean = True

    Private Property TeamView() As Node.TreeType
        Get
            If String.IsNullOrEmpty(My.Settings.TeamView) Or IsNumeric(My.Settings.TeamView) Then
                My.Settings.TeamView = Node.TreeType.Iteration.ToString
                My.Settings.Save()
            End If
            Return CType([Enum].Parse(GetType(Node.TreeType), My.Settings.TeamView), Node.TreeType)
        End Get
        Set(ByVal value As Node.TreeType)
            My.Settings.TeamView = value.ToString
            My.Settings.Save()
        End Set
    End Property

    Private Sub RefreshTeamViews()
        ux_TeamViewDDL.IsEnabled = True
        '-------------------------
        ux_TeamViewDDL.DataContext = [Enum].GetValues(GetType(Node.TreeType))
        Me.ux_TeamViewDDL.SelectedValue = TeamView
        '-------------------------
    End Sub

    Private Sub ux_TeamViewDDL_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ux_TeamViewDDL.SelectionChanged
        TeamView = CType(e.AddedItems(0), Node.TreeType)
        If m_TeamViewInital Then
            m_TeamViewInital = False
        Else
            Refresh()
        End If
    End Sub

#End Region

#End Region

    Private Sub ActionPanel_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        RefreshTeamViews()
        RefreshTeamServers()
    End Sub

    Public Sub Refresh()
        '-------------------------
        If Not Server Is Nothing And Not Project Is Nothing Then
            RaiseEvent TeamSelectionChanged(Me, New TeamRoutedEventArgs(TeamSelectionEvent, Me, Project, TeamView))
        End If
    End Sub


End Class
